﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProduceBasicData.Api.Application.Command.RBAC.Permission;
using ProduceBasicData.Api.Application.Command.RBAC.Permission;
using ProduceBasicData.Api.DTO.RBACDto;
using ProduceBasicData.ErrorCode;

namespace ProduceBasicData.Api.Controllers.RBAC
{
    /// <summary>
    /// 权限控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PermissionsController : ControllerBase
    {
        private readonly IMediator _mediator;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator"></param>
        public PermissionsController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 创建权限
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreatePermission(CreatePermissionCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 获取权限列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PermissionsDto>> GetPermissionList([FromQuery] GetPermissionCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 获取所有权限列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<PermissionsDto>>> GetAllPermissionList([FromQuery] GetAllPermissionCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 判断是否有子权限
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<bool>> HasChildPermission([FromQuery] HasChildPermissionCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 更新权限
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdatePermission(UpdatePermissionCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> DeletePermission(DeletePermissionCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 权限批量删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> DeleteRangePermission(DeleteRangePermissionCommand command)
        {
            return await _mediator.Send(command);
        }
    }
}
